<?php namespace Home\Controller;
//use Hdphp\Controller\Controller;
/**
 * 购物车控制器
 */
class CartController extends CommonController {	
	public function index() {
		if (!empty($_SESSION['cart'])) {
			$cartData = $_SESSION['cart']['goods'];
			$count = count($cartData);
			foreach ($cartData as $k => $v) {
				$cartData[$k]['options'] = explode(',', $v['options']);
			}
//			p($_SESSION);
			//分配数据
		View::with('count',$count);
		View::with('session',$_SESSION);
		View::with('cartData',$cartData);
		}			
		View::make();
	}
	public function getAjaxattr(){
		if(IS_AJAX)
		{
			$num = $_POST['num'];
			$sid = $_POST['sid'];
			$data=array( 
            'sid'=>$sid,// 唯一 sid，添加购物车时自动生成 
            'num'=>$num,
            ); 
            Cart::update($data);
			echo json_encode($a=1);
		}
	}
	//异步单个删除
	public function Ajaxdel(){
		if(IS_AJAX)
		{
			$sid = $_POST['sid'];
			Cart::del($sid);
			echo json_encode($a=1);
		}
	}
	//异步多个删除
    public function allDelAjax(){
    	if(IS_AJAX)
		{
			$a = $_POST['a'];
			$a = rtrim($a,',');
			$a = explode(',', $a);
			foreach($a as $k=>$v)
			{
				Cart::del($v);
			}
			echo json_encode($phpdata=1);
		}
    }
}
